package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.BigDisasterEntity;
import com.css.fxfzaqbz.modules.assess.repository.BigDisasterRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author y1fan
 * @create 2022-09-16 16:35
 */
@Repository
public class BigDisasterRepositoryImpl implements BigDisasterRepository {


    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public void saveBigDisaster(BigDisasterEntity bigDisasterEntity) {
        String sql = "insert into sys_big_disaster  (bd_id,bd_activeid,bd_opinion,bd_status,bd_createtime,bd_del_flag,bd_createuser,content)values(:bdId,:bdActiveId,:bdOpinion,:bdStatus,:bdCreateTime,:bdDelFlag,:bdCreateUser,:content)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(bigDisasterEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public BigDisasterEntity getBigDisasterByActiveId(String sdaRelation) {
        String sql="";
        sql="select u.* from sys_big_disaster u  where  u.bd_activeid='"+sdaRelation+"' and u.bd_del_flag = '"+ YNEnum.N.toString() +"'";
        BigDisasterEntity bigDisasterEntity = null;
        try {
            bigDisasterEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BigDisasterEntity.class));
            return bigDisasterEntity;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateBigStatus(String bbId) {
        String sql = "update sys_big_disaster set" +
                " bd_status = '"+YNEnum.Y+"' " +
                " where bd_id = '" + bbId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_big_disaster set" +
                " bd_status = '"+2+"' " +
                " where bd_activeid = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
